package com.github.xiaoy.aop;

import org.springframework.aop.support.NameMatchMethodPointcut;
import org.springframework.aop.support.StaticMethodMatcherPointcut;
import org.springframework.util.PatternMatchUtils;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class NameMatchMethodPointcutCustom extends StaticMethodMatcherPointcut implements Serializable {

    private List<String> mappedClassNames = new ArrayList<>();


    /**
     * Convenience method when we have only a single method name to match.
     * Use either this method or {@code setMappedNames}, not both.
     * @see #setMappedNames
     */
    public void setMappedName(String mappedName) {
        setMappedNames(mappedName);
    }

    /**
     * Set the method names defining methods to match.
     * Matching will be the union of all these; if any match,
     * the pointcut matches.
     */
    public void setMappedNames(String... mappedNames) {
        this.mappedClassNames = new ArrayList<>(Arrays.asList(mappedNames));
    }

    /**
     * Add another eligible method name, in addition to those already named.
     * Like the set methods, this method is for use when configuring proxies,
     * before a proxy is used.
     * <p><b>NB:</b> This method does not work after the proxy is in
     * use, as advice chains will be cached.
     * @param name name of the additional method that will match
     * @return this pointcut to allow for multiple additions in one line
     */
    public NameMatchMethodPointcutCustom addMethodClassName(String name) {
        this.mappedClassNames.add(name);
        return this;
    }


    @Override
    public boolean matches(Method method, Class<?> targetClass) {
        for (String mappedName : this.mappedClassNames) {
            if (mappedName.equals(method.getDeclaringClass().getSimpleName()) || isMatch(method.getDeclaringClass().getName(), mappedName)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Return if the given method name matches the mapped name.
     * <p>The default implementation checks for "xxx*", "*xxx" and "*xxx*" matches,
     * as well as direct equality. Can be overridden in subclasses.
     * @param methodName the method name of the class
     * @param mappedName the name in the descriptor
     * @return if the names match
     * @see org.springframework.util.PatternMatchUtils#simpleMatch(String, String)
     */
    protected boolean isMatch(String methodName, String mappedName) {
        return PatternMatchUtils.simpleMatch(mappedName, methodName);
    }


    @Override
    public boolean equals(Object other) {
        return (this == other || (other instanceof NameMatchMethodPointcut &&
                this.mappedClassNames.equals(((NameMatchMethodPointcutCustom) other).mappedClassNames)));
    }

    @Override
    public int hashCode() {
        return this.mappedClassNames.hashCode();
    }

}
